<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="howl.types.*"%>
<%@ page import="howl.types.query.*"%>
<%@ page import="java.util.List"%>
<%@ page import="howl.Twitter.EnableTwitter"%>
<%@ page import="javax.jdo.Query"%>
<%@ page import="howl.Twitter.TwitterProvider"%>
<%@ page import="twitter4j.Twitter"%>
<%@ page import="twitter4j.http.RequestToken"%>
<%@ page import="java.util.Date"%>


<%
TableDescriptor td = null;
String name = request.getParameter("id");
List<DataProvider> sensors = null;
if(name != null){

	UserService userService1 = UserServiceFactory.getUserService();
	User user1 = userService1.getCurrentUser();
	HowlUser hu = Model.getHowlUserfromUser(user1);
	Model m = new Model(hu);
	
	td = m.getTableDescriptor(name); 
		
	
	if(td == null){
	response.sendRedirect("index.jsp");
	}

	
	
	sensors = (List<DataProvider>) m.getDataProvider(td);
	
	}else {
	response.sendRedirect("index.jsp");
	}


%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ include file="loggedIn.jsp" %>
<%@ include file="VisualizationJS.jsp"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Howl: a web service for the Internet of Things</title> 
<link rel="stylesheet" type="text/css" href="/style.css"/>
<script
	src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAAhuCR-XpauT1569wTOZf_VBRdiqBSwNOJIPrxtYsnQB8f-qb1wRT8SF-TuKeWjzEsNXRmR_PNVQvbeQ"></script>
<script type="text/javascript">

    function initialize(arg1,arg2,arg3) {
    
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById(arg3.id));
        map.setCenter(new GLatLng(arg1, arg2), 13);
        
        var point = new GLatLng(arg1,
                              arg2);
      map.addOverlay(new GMarker(point));
      }
    }

    
    </script>
<%=getJs(name) %>

</head>
<body>

  <div id="commands">
    <a class="command" href="/Auth/QuickEntry.jsp">Run commmand</a> 
    <a class="command" href="<%= userService.createLogoutURL("/Welcome.jsp") %>">Sign out</a> 
  </div>  <!-- end commands -->
<div id="page">

 <div id="TopBanner"> 
	<p>Howl: a web service for the Internet of Things</p>
  </div> <!-- end TopBanner -->
  
<div id="menu">
<%@ include file="menu.jsp"%>
</div><!-- end menu -->
<div id="content">

<div id="sensorresults"> 
<div>

<div id="feedName">
	<strong>Feed</strong>: <%=td.getName() %>
</div>  <!-- end feedName -->


<div id="visualization" style="width: 600px; height: 400px;">
</div> <!-- end visualization -->
<div id="linkButtons">
<a href="<%="/Auth/EditAccess.jsp?Feed=" +td.getName() %>"><input type="button" value="Share"/></a>
<%
String xmlurl = "/XML/" +td.getName().split("@")[1] + "/" + td.getName().split("@")[0] +".xml";
String csvurl = "/CSV/" +td.getName().split("@")[1] + "/" + td.getName().split("@")[0] + ".csv";
String jsonurl = "/JSON/" +td.getName().split("@")[1] + "/" + td.getName().split("@")[0] + ".json";

%>
<a href="<%=xmlurl %>"><img src="/buttons/xml.gif"/></a>
<a href="<%=csvurl %>"><img src="/buttons/csv.gif"/></a>
<a href="<%=jsonurl %>"><img src="/buttons/json.gif" style=" width: 36px; height: 14px"/></a>
</div><!-- endlinkButtons -->
<div id="feedSchema">
<Table>
	<tr>
		<th align="left">Name</th>
		<th>Unit</th>
		<th>Value</th>
	</tr>
	<tr>
		<%
	for(int i = 0; i<td.getStringmapper().size();i++){
	%>
	
	<tr>
		<td align="left"><%=td.getStringmapper().get(i) %></td>
		<td align="center"><%=td.getUnitmapper().get(i) %></td>
		<%
		ResultSet rs = (QueryHelper.queryByEntity(name, 1));
		if(rs.hasNext()){
		
			
			try{
				%>
				<td align="center"><%= ((Result)rs.next()).getValue(td.getStringmapper().get(i)).getValue() %></td>
				<%
			
			} catch (Exception e){
				 %>
					<td align="center"><%="Data was not submitted" %></td>
					<%
			}
		} else {
		 %>
		 <td align="center">No Data recorded yet</td>
		 <%} %>
		
	</tr>
	<%
	}
	%>
	</tr>


</Table>
</div> <!-- end feedSchema -->


<div id="feedLocation">
<Table width="100%" border="1">
	<tr>
		<th>Name</th>
		<th>Latitude</th>
		<th>Longitude</th>
		<th>Location</th>
	</tr>
	<%
for(int j = 0; j<sensors.size();j++){
	%>
	<tr>
		<td><%=sensors.get(j).getName() %></td>
		<td><%=sensors.get(j).getLatitude() %></td>
		<td><%=sensors.get(j).getLongitude() %></td>
		<td align="center" rowspan="2">
		<div class="feedMap" id="<%=sensors.get(j).getName() %>" style="width: 300px; height: 150px;"></div>
		
		<script type="text/javascript">
			initialize(<%=sensors.get(j).getLongitude() %>,<%=sensors.get(j).getLatitude() %>,<%=sensors.get(j).getName() %>);
		</script>
		</td>
		
	</tr>
	<tr></tr>
	<tr></tr>
	<%
}


 %>
</Table>
</div> <!-- end feedLocation -->
</div><!-- end sensorresults -->
</div>
</div> <! -- end page -->
<div id="footer">
	<p>Copyright University of Wolverhampton &copy; 2009</p>
</div> <!-- end footer -->
</body>
</html>